<?php
namespace App\Adshopuser\Models;
use Common\BaseModel;
class ShopRoles extends BaseModel
{
    protected $res;
    protected $redis_key="admin_";
    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_roles');
    }



    /**
     * @return mixed获取所有权限组
     */
    public function getlist($params=[]){

        if(!isset($params['id']) && empty($params['id'])) {
        $cate_list = $this::find(
            array(
                "order" => 'id asc',
            ));
        }else{
            $cate_list = $this::findFirstById($params['id']);
        }
        return $cate_list;
    }


    /**
     * 获取权限组的名称
     * @param $ids
     * @return mixed
     */
    public function find_by_ids($ids){
        $condition = "id in ({$ids})";
        $ids_obj = $this->find(
            array(
                $condition
            )
        );
        if($ids_obj){
            return $ids_obj->toarray();
        }else{
            return [];
        }
    }

    public function find_by_id($id){
        return $this->findFirstById($id);
    }


    /**
     * @func 根据手机号获取用户的信息
     */
    public function find_by_phone($phone){
        $user_info = $this->findFirstByPhone($phone);
        if(!$user_info){
            return false;
        }else{
            return $user_info->toarray();
        }
    }



    /**
     * @func 根据用户id获取用户信息
     */
    public function find_by_firstId($ids){
        $conditions = "id in({$ids})";
        $user_info = $this->find(array(
            $conditions
        ))->toarray();
        if(in_array('1',explode(',',$ids))){    //1是超级管理员权限组
            $rules=[];
        }else {
            $rules_ids='';
            foreach ($user_info as $key => $value) {
                if(!empty($value['rules'])){
                    if($key == 0){
                        $rules_ids .= $value['rules'];
                    }else{
                        $rules_ids .= ','.$value['rules'];
                    }
                }
                if($rules_ids && $value['b_rules']){
                    $rules_ids .= $value['rules'].','.$value['b_rules'];
                }
            }
            if(!empty($rules_ids)){
                $rules_ids = explode(',',$rules_ids);
                sort($rules_ids);
                $rules = implode(',',array_unique($rules_ids));
            }else{
                $rules = '-1';
            }
        }
        $adminrule = new ShopRule();
        $rule_list = $adminrule->rulelist($rules);
        return $rule_list;
    }

    /**
     * @func 根据用户名查找用户
     */
    public function find_by_name($name){
        $user_info = $this->findFirstByName($name);
        if(!$user_info){
            return false;
        }else{
            return $user_info->toarray();
        }
    }

    /**
     * @func 根据邀请码获取用户
     */
    public function find_by_inviteCode($invite_code){
        $conditions['conditions'] = "invite_code = '{$invite_code}' or  phone = '{$invite_code}'";
        $user_info = $this->findFirst($conditions);
        if(!$user_info){
            return false;
        }else{
            return $user_info->toarray();
        }
    }


    /**
     * @func 更新/插入数据
     * @param $data要更新或者要插入的数据
     * @return boolean 插入成功的结果
     */
    public function save_user($data){
        $data['updated_at'] = date('Y-m-d H:i:s',time());
        $result = $this->save($data);
        return $result;
    }
}
